import { Application } from "egg";
import * as AutoIncrementFactory from "mongoose-sequence";
import { ObjectId } from "mongoose";
export interface DrugProps {
  _id: ObjectId;
  name: string; // 药品名称
  brand: string; // 药品品牌
  standard: string; // 药品规格
  price: number; // 药品销售价
  entryPrice: number; // 药品进价
  factory: string; // 药品生产厂商
  is_delete?: boolean; // 是否删除
}
function initDrugModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const DrugScheme = new Schema<DrugProps>(
    {
      name: { type: String, required: true }, // 名称
      brand: { type: String, required: true }, // 品牌
      standard: { type: String, required: true }, // 规格
      price: { type: Number, default: 0 }, // 售价
      entryPrice: { type: Number, default: 0 }, // 进价
      factory: { type: String, required: true }, // 生产厂家
      is_delete: { type: Boolean, default: false },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  DrugScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Drug_counter",
  });
  return mongoose.model<DrugProps>("Drug", DrugScheme);
}

export default initDrugModel;
